﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CYTS.Aoyou.Framework.Utility
{
    /// <summary>
    /// 转换帮助类
    /// </summary>
    public sealed class ConvertHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool ToBoolean(object value, bool defaultValue)
        {
            bool result = defaultValue;

            if (value != null)
            {
                if (Validator.IsBoolean(value))
                {
                    string str = value.ToString();

                    if (Validator.NumberIsTrue(str))
                    {
                        result = true;
                    }
                    else
                    {
                        result = bool.Parse(str);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string ToString(object value, string defaultValue)
        {
            if (value == null)
            {
                return defaultValue;
            }

            return value.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ToInt32(object value, int defaultValue)
        {
            int result = defaultValue;

            if (value != null)
            {
                if (Validator.IsInt32(value))
                {
                    string str = value.ToString();

                    result = int.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(object value, DateTime defaultValue)
        {
            DateTime result = defaultValue;

            if (value != null)
            {
                if (Validator.IsDateTime(value))
                {
                    string str = value.ToString();

                    result = DateTime.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static char ToChar(object value, char defaultValue)
        {
            char result = defaultValue;

            if (value != null)
            {
                if (Validator.IsChar(value))
                {
                    string str = value.ToString();

                    result = char.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static short ToInt16(object value, short defaultValue)
        {
            short result = defaultValue;

            if (value != null)
            {
                if (Validator.IsInt16(value))
                {
                    string str = value.ToString();

                    result = short.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ToInt64(object value, long defaultValue)
        {
            long result = defaultValue;

            if (value != null)
            {
                if (Validator.IsInt64(value))
                {
                    string str = value.ToString();

                    result = long.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static double ToDouble(object value, double defaultValue)
        {
            double result = defaultValue;

            if (value != null)
            {
                if (Validator.IsDouble(value))
                {
                    string str = value.ToString();

                    result = double.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static float ToFloat(object value, float defaultValue)
        {
            float result = defaultValue;

            if (value != null)
            {
                if (Validator.IsFloat(value))
                {
                    string str = value.ToString();

                    result = float.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object value, decimal defaultValue)
        {
            decimal result = defaultValue;

            if (value != null)
            {
                if (Validator.IsDecimal(value))
                {
                    string str = value.ToString();

                    result = decimal.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Guid ToGuid(object value, Guid defaultValue)
        {
            Guid result = defaultValue;

            return defaultValue;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static byte ToByte(object value, byte defaultValue)
        {
            byte result = defaultValue;

            if (value != null)
            {
                if (Validator.IsByte(value))
                {
                    string str = value.ToString();

                    result = byte.Parse(str);
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static TimeSpan ToTimeSpan(object value, TimeSpan defaultValue)
        {
            TimeSpan result = defaultValue;

            if (value != null)
            {
                if (Validator.IsTimeSpan(value))
                {
                    string str = value.ToString();

                    result = TimeSpan.Parse(str);
                }
            }

            return result;
        }
    }
}
